#include<event2/event.h>
#include<signal.h>
#include<iostream>
#include<string.h>
#include <fcntl.h>
#include<unistd.h>
using namespace std;

void readfile_cb(evutil_socket_t sock,short event,void *arg)
{
   char buff[1024];
   memset(buff,0,1024);
   int len = read(sock,buff,sizeof(buff)-1);
   if(len>0)
   {
        cout<<buff<<endl;
   }
   else
   {

   }
  
}
int main(int argc,char **argv)
{
    // event_config *ec= event_config_new();
    // event_base *eb=event_base_new_with_config(ec);

    // event_base_free(eb);
    // event_config_free(ec);
    if(signal(SIGPIPE,SIG_IGN)==SIG_ERR)
    {
        return 1;
    }
    event_config *ec=event_config_new();
    //设置属性：支持文件描述符
    event_config_require_features(ec,EV_FEATURE_FDS);
    event_base *eb=event_base_new_with_config(ec);
    event_config_free(ec);
    if(!eb)
    {
        cerr<<"event_base_new_with_config failed"<<endl;
        return -1;
    }
    //打开文件只读非阻塞
    int sock=open("/var/log/auth.log",O_RDONLY|O_NONBLOCK,0);
    if(sock<=0)
    {
        cerr<<"open failed"<<endl;
        return -2;
    }
    //文件指针移到文件结尾处
    lseek(sock,0,SEEK_END);
    //监听文件数据
    event *fev=event_new(eb,sock,EV_READ|EV_PERSIST,readfile_cb,0);
    event_add(fev,NULL);

    //进入事件主循环
    event_base_dispatch(eb);
    event_base_free(eb);
    
    
    return 0;
}